﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows;
using System.Threading;

namespace Hugo.UIPages
{
    class SkypeIncomingCallUIPage : UIPage
    {
        private string displayName;

        Rectangle accept;
        Rectangle reject;
        private int indexOfSelectedImage;
        private System.Media.SoundPlayer sound;
        private bool calltransfered = false;
        private Grid g;

        public SkypeIncomingCallUIPage(String displayName)
            : base("Incoming call", Util.getBitmapSource(Properties.Resources.skype))
        {
            Console.WriteLine("Looking up contact " + displayName);
            //com.skype.api.Contact c = Communicate.getInstance().getContactWithDisplayName(displayName);

            this.displayName = displayName;
            
            Label instructionsLabel = new Label();
            instructionsLabel.Content = Translate.translate("Call from ") + displayName.Trim();
            instructionsLabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            instructionsLabel.Foreground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.White);
            instructionsLabel.FontSize = GlobalSettings.SelectedLabelFontSize;
            instructionsLabel.Margin = new System.Windows.Thickness(0, GlobalSettings.EffectiveScreenMiddleY-250, 0, 0);

            //Rectangle rcaller = Util.getSymbol(Util.getImageForContact(c));

            g = new Grid();
            g.Width = GlobalSettings.ScreenMiddleX * 2;
            PageCanvas.Children.Add(g);

            g.Children.Add(instructionsLabel);

            accept = Util.getSymbol(Util.getBitmapSource(Properties.Resources.callaccept));
            reject = Util.getSymbol(Util.getBitmapSource(Properties.Resources.calldecline));

            //PageCanvas.Children.Add(rcaller);
            PageCanvas.Children.Add(accept);
            PageCanvas.Children.Add(reject);

            //Canvas.SetTop(rcaller, GlobalSettings.EffectiveScreenMiddleY);
            //Canvas.SetLeft(rcaller, GlobalSettings.ScreenMiddleX - rcaller.Width/2);
            
            Canvas.SetTop(accept, GlobalSettings.EffectiveScreenMiddleY - 100);
            Canvas.SetTop(reject, GlobalSettings.EffectiveScreenMiddleY - 100);
            Canvas.SetLeft(accept, GlobalSettings.ScreenMiddleX - accept.Width - 10);
            Canvas.SetLeft(reject, GlobalSettings.ScreenMiddleX + 10);

            this.isBackButtonVisible = false;
        }

        public override bool respondToClick(System.Windows.Input.MouseButtonEventArgs e)
        {
            if (indexOfSelectedImage != -1)
            {
                // accept
                if (indexOfSelectedImage == 0)
                {

                    PageCanvas.Opacity = 0;

                    Thread Messagethread = new Thread(
                    new ThreadStart(delegate()
                    {
                        MainWindow.getInstance().Dispatcher.Invoke(
                          System.Windows.Threading.DispatcherPriority.Normal,
                          new Action(
                            delegate()
                            {
                                Communicate.mySession.callActive = true;
                                //Console.WriteLine("H set callActive to " + Communicate.mySession.callActive);
                                Console.WriteLine("Conv: Call answered!");

                                g.Opacity = 0;
                                MainWindow.pushPage(new SkypeEndCallUIPage(displayName));
                                //MainWindow.removePage(this);
                            }
                        ));
                    }));
                    Messagethread.SetApartmentState(ApartmentState.STA);
                    Messagethread.Start();
                }
                // reject
                else
                {
                    Communicate.activeParticipant.hangup();
                    Console.WriteLine("B hungup");
                    MainWindow.popPage();
                }

                this.sound.Stop();

                return true;
            }

            return false;
        }

        public override void setPage()
        {
            this.sound = Sounds.playSound(Sounds.Sound.IncomingCall, true);
            base.setPage();
        }

        public override bool setCursorPosition(System.Windows.Point p)
        {
            int ind = getIndexOfImageAtPosition(p);

            if (ind != -1)
            {
                return true;
            }

            return false;
        }

        private int getIndexOfImageAtPosition(System.Windows.Point p)
        {
            int index = -1;

            int i = 0;
            foreach (Rectangle o in new List<Rectangle> { accept, reject })
            {
                Rectangle im = o;

                Point pos = im.PointToScreen(new Point());

                double rX = pos.X - HandCursor.handSize / 2;
                double rY = pos.Y - HandCursor.handSize / 2;
                double rX2 = rX + im.ActualWidth;
                double rY2 = rY + im.ActualHeight;

                if (rX < p.X && rY < p.Y && rX2 > p.X && rY2 > p.Y)
                {
                    index = i;
                    break;
                }
                i++;
            }

            indexOfSelectedImage = index;

            return index;
        }

    }
}
